import subprocess

import requests


# def ping(url):
#     try:
#         result = requests.get(url, timeout=3)
#         if result.status_code == 200:
#             return True
#     except Exception as e:
#
#         return False

def ping(ip_address):
    # 使用ping命令检查IP是否可达
    command = ['ping', '-c', '1', ip_address]  # 在Linux/MacOS上使用'-c'参数，在Windows上使用'-n'参数
    try:
        # 执行ping命令
        subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        # 如果ping成功，则返回True
        return True
    except subprocess.CalledProcessError:
        # 如果ping失败，则返回False
        return False


if __name__ == "__main__":
    print(ping('https://www.baidu.com'))
    print(ping('192.168.1.1'))
